<?php

include(dirname(__FILE__).'/../../bootstrap/functional.php');

$browser = new CmsTestFunctional(new sfBrowser());
$browser->loadData();

$browser->info('1 - Strona startowa')->
  get('/')->

  with('request')->
  begin()->
    isParameter('module', 'field')->
    isParameter('action', 'index')->
    isParameter('sf_culture', 'pl')->
    isParameter('table', 'entry')->
  end()->

  with('response')->
  begin()->
    isStatusCode(200)->
  end()
;

$max = sfConfig::get('app_max_records_per_page');
 
$browser->
  info(sprintf('  1.1 - Tylko %s wpisów jest widocznych w tabeli głównej', $max))->
  with('response')->
  begin()->
    checkElement('tbody tr', $max)->
  end()
;
 
$browser->info('1.2 - Sortowanie')->
  click('identyfikator', array(), array('position' => 1))->
  with('request')->
  begin()->
    isParameter('module', 'field')->
    isParameter('action', 'order')->
    isParameter('orderBy', 'id')->
  end()->
        
  with('response')->
  begin()->
    isStatusCode(200)->
  end()
;

$CmsEntry = Doctrine_Core::getTable('CmsEntry')->createQuery('c')->select('max(c.created_at), min(c.created_at)')->execute()->toArray();

$browser->info(sprintf('1.2.1 - Sortowanie po dacie utworzenia min: %s, max: %s', $array[0]['min'], $array[0]['max']))->
        
  click('utworzono', array(), array('position' => 1))->             
  with('response')->
  begin()->
    checkElement('tbody tr:nth(0) td[name="created_at"]', $CmsEntry[0]['min'])->
  end()->
        
  click('utworzono', array(), array('position' => 1))-> 
  with('response')->
  begin()->
    checkElement('tbody tr:nth(0) td[name="created_at"]', $CmsEntry[0]['max'])->
  end()->
        
  click('utworzono', array(), array('position' => 1))->             
  with('response')->
  begin()->
    checkElement('tbody tr:nth(0) td[name="created_at"]', $CmsEntry[0]['min'])->
  end()
;


$array = Doctrine_Core::getTable('CmsMenu')->createQuery('c')->select('max(c.name), min(c.name)')->execute()->toArray();

$browser->info(sprintf('1.2.2 - Sortowanie po nazwie menu min: %s, max: %s', $array[0]['min'], $array[0]['max']))->
        
  click('menu', array(), array('position' => 2))->             
  with('response')->
  begin()->
    checkElement('tbody tr:nth(0) td[name="menu_id"]', $array[0]['min'])->
  end()->
        
  click('menu', array(), array('position' => 2))-> 
  with('response')->
  begin()->
    checkElement('tbody tr:nth(0) td[name="menu_id"]', $array[0]['max'])->
  end()->
        
  click('menu', array(), array('position' => 2))->             
  with('response')->
  begin()->
    checkElement('tbody tr:nth(0) td[name="menu_id"]', $array[0]['min'])->
  end()
;

$browser->setHttpHeader('X_REQUESTED_WITH', 'XMLHttpRequest');
$browser->info('1.3 - Ustawianie pozycji wpisu za pomocą ui-sortable')->

  post('/set_position', array(
        'tableOrderArray' => array('2','1','3','4','5','6','7'),          
        'table'           => 'entry',
        'currentPage'     => '1'
      ))->
  get('/')->     
  click('pozycja', array(), array('position' => 1))->    
  with('response')->   
  begin()->
    isStatusCode(200)->
    checkElement('tbody tr:nth(0) td[name="id"] a', '2')->
  end()
;

$browser->info('1.4 - Wyszukiwanie za pomocą table-search-widget')->
  post('/pl/table_search',array(
      'fields'=>array(
          '0'=>array(
              'columnName'=>'type_id',
              'relationName'=>'cmsEntryType',
              'type'=>'choice', 
              'value'=>'1'
          ),
          '1'=>array(
              'columnName'=>'menu_id', 
              'relationName'=>'cmsMenu',
              'type'=>'choice', 
              'value'=>'2'
          ),
          '2'=>array(
              'columnName'=>'is_active', 
              'type'=>'boolean', 
              'value'=>'1'
          ),
          '3'=>array(
              'columnName'=>'id', 
              'type'=>'text', 
              'value'=>'4'
          )
        ), 
        'table'=>'entry', 
        'page'=>'1'
      ))->
        
  with('response')->
  begin()->
    isStatusCode(200)->
    checkElement('tbody tr',1)->
  end()
;